* This do-file creates demand curves from the seed auction

*****
* Construct Demand at Auction Price Data from Raw Files
*****
forvalues season = 0/1 {

if `season'==0 {
	* Import Kharif Data
	use "$bdm_raw/Seed_Auction_Kharif_2019.dta", clear 
	* Identify pulse varieties
	local crops bg pp
	local shapes "quantZ_bg quantZ_pp"
	local keepers "quantZ_bg* quantZ_pp* farmer_old participate*"
	local renames "(quantZ_pp quantZ_bg) (quantZ1 quantZ2)"
	
	* Fix transcription errors in (new) data
	destring hhid_key, gen(temp1)
	replace hhid = temp1 if !missing(hhid_key)
	destring hhid_new, gen(temp2)
	replace hhid = temp2 if !missing(hhid_new)
	drop temp*
}
else if `season'==1 {
	* Import Rabi data
	use "$bdm_raw/Seed_Auction_Rabi_2020.dta", clear
	* Identify pulse varieties
	local crops ln
	local shapes "quantZ_ln"
	local keepers "quantZ_ln* farmer_old participate*"
	local renames "quantZ_ln quantZ3"
}
local covs gender age caste relationship marital_status education literacy hh_size 

* Get consistent district, block, and village numbers
foreach geog in district block village {
	label val `geog'_code `geog'_new
}

* Flag participation in other surveys and each seed auction
gen newfarmer=0
replace newfarmer=1 if hh_new!=.

gen include_in_study = participate

* Replace missing values with 0 demand at price
forvalues i=60(20)140 {
	foreach crop in `crops' {
		gen quantZ_`crop'`i' = quant_`crop'`i'
		replace quantZ_`crop'`i'=0 if missing(quant_`crop'`i') & include_in_study==1
	}
}

* Add in saved seeds, very messy 
	* Convert everything to kgs (assume kg unless g and qty>=100)
	rename *_quantity_* *_qty_*
	destring *_qty_* *_unit_*, replace
	foreach v of varlist *_qty_* {
		local vu = subinstr("`v'","qty","unit",1)
		replace `v' = `v'/1000 if `vu'==3 & `v'>=100
	}

if `season'==0 {
	* Classify main variety questions (B1)
	destring seed_code_*, replace
	foreach src in saved saved_oth purchased {
	forvalues i = 1/3 {
		gen pigeon_`src'_`i' = `src'_qty_`i' if inlist(seed_code_`i',11, 12, 14, 21, 22, 32, 35, 42, 45, 51, 52, 53, 54, 91, 93)
		gen blackg_`src'_`i' = `src'_qty_`i' if inlist(seed_code_`i',16, 26, 36, 37, 46, 47, 56, 92, 94)
		gen puloth_`src'_`i' = `src'_qty_`i' if seed_code_`i'==95
	}
	}

	* Add it all up
	rename pulse_plan_* pulseplan_*
	egen saved_seed1 = rowtotal(pigeon_* oth_*_qty_1)
	egen saved_seed2 = rowtotal(blackg_* oth_*_qty_2)
	egen saved_seed_misc = rowtotal(puloth_* pulse_*_qty_*)
	rename pulseplan_* pulse_plan_*
}
else if `season==1' {
	* Add it up
	egen saved_seed3 = rowtotal(saved_qty_* saved_oth_qty_* purchased_qty_*)
	egen saved_seed_misc = rowtotal(pulse_saved_qty_* pulse_saved_oth_qty_* pulse_purchased_qty_*)
}

* Keep stuff and reshape long over prices
keep hhid district_code block_code village_code treat_code newfarmer include_in_study saved_seed* `keepers' `covs'
reshape long `shapes', i(hhid) j(price)

* Rename and reshape long over crops
rename `renames'
reshape long quantZ saved_seed, i(hhid price) j(crop)

* Put together all data and label crops
if `season'==1 {
	append using "$out_data/auction_price_data.dta"
	cap label define crop_lbl 1 "Pigeon Pea" 2 "Black Gram" 3 "Lentil"
	label val crop crop_lbl
	sort crop hhid price
	order district_code block_code village_code hhid crop price quantZ treat_code newfarmer include_in `covs'
}
save "$out_data/auction_price_data.dta", replace
}
